package statis

import (
	"fmt"

	"qkstatis/db"

	"doubimeizhi.com/datatable"
	"doubimeizhi.com/utility"

	log "github.com/cihub/seelog"
)

var allProfitStatisInstance *AllProfitStatisService

type AllProfitStatisService struct{}

func GetAllProfitStatisService() *AllProfitStatisService {
	if allProfitStatisInstance == nil {
		allProfitStatisInstance = &AllProfitStatisService{}
	}
	return allProfitStatisInstance
}

//查询列表
func (p *AllProfitStatisService) ListAllProfit(ltype int, conditions map[string]interface{}, needTotalRecords bool) ([]*AllProfit, int) {
	dtq := p.allProfitDtq(ltype)
	//dtq.SetPage(start, length)
	for key, val := range conditions {
		dtq.PushCondition(key, val)
	}
	sql, values := dtq.GenerateSQL()
	data := make([]*AllProfit, 0)
	mysqlHelper := db.GetMySQLHelper()
	if results, err := mysqlHelper.Query(sql, values...); err == nil {
		if len(results) == 0 {
			log.Warnf("查询没有统计数据，conditions:%v", conditions)
			return make([]*AllProfit, 0, 0), 0
		}
		for _, dt := range results {
			data = append(data, p.allProfitParseRecords(dt, mysqlHelper))
		}
	}
	recordsTotal := 0
	if needTotalRecords {
		s, v := dtq.GenerateTotalRecordSQL("total_records")
		if results, err := mysqlHelper.Query(s, v...); err == nil {
			if len(results) > 0 {
				recordsTotal = mysqlHelper.GetColInt(results[0], "total_records")
			}
		} else {
			log.Errorf("mysql err sql:%s, values:%v", err.Error(), v)
		}
	}
	return data, recordsTotal
}

func (p *AllProfitStatisService) allProfitDtq(ltype int) *datatable.DataTableQuery {
	dtq := datatable.NewDataTableQuery()
	dtq.PushField(&datatable.FiledItem{Alias: "date_flag", Field: "date_flag", OrderBy: datatable.DESC})
	dtq.PushField(&datatable.FiledItem{Alias: "washing_orders", Field: "washing_orders"})
	dtq.PushField(&datatable.FiledItem{Alias: "recharge_orders", Field: "recharge_orders"})
	dtq.PushField(&datatable.FiledItem{Alias: "washing_peer", Field: "washing_peer"})
	dtq.PushField(&datatable.FiledItem{Alias: "recharge_peer", Field: "recharge_peer"})
	dtq.PushField(&datatable.FiledItem{Alias: "washing_total", Field: "washing_total"})
	dtq.PushField(&datatable.FiledItem{Alias: "recharge_total", Field: "recharge_total"})
	dtq.PushField(&datatable.FiledItem{Alias: "total", Field: "total"})
	dtq.PushField(&datatable.FiledItem{Alias: "washing_discount", Field: "washing_discount"})
	dtq.PushField(&datatable.FiledItem{Alias: "recharge_discount", Field: "recharge_discount"})
	dtq.PushField(&datatable.FiledItem{Alias: "washing_cash_discount", Field: "washing_cash_discount"})
	dtq.PushField(&datatable.FiledItem{Alias: "washing_qb_discount", Field: "washing_qb_discount"})
	dtq.PushField(&datatable.FiledItem{Alias: "washing_cash_part", Field: "washing_cash_part"})
	dtq.PushField(&datatable.FiledItem{Alias: "washing_qk_part", Field: "washing_qk_part"})
	switch ltype {
	case 1:
		dtq.SetTableName("statis_all_profit_day")
	case 2:
		dtq.SetTableName("statis_all_profit_week")

	case 3:
		dtq.SetTableName("statis_all_profit_month")
	default:
		log.Errorf("allProfitDtq, 未知参数ltype； ltype:%d", ltype)
		dtq.SetTableName("none")
	}
	return dtq
}

func (p *AllProfitStatisService) allProfitParseRecords(dt map[string]interface{}, mysqlHelper *utility.MySQLHelper) *AllProfit {
	ap := &AllProfit{}
	ap.DateFlag = mysqlHelper.GetColString(dt, "date_flag")
	if len(ap.DateFlag) == 6 {
		ap.DateFlag += GetStatisService().date2week(ap.DateFlag)
	}
	ap.WashingOrders = mysqlHelper.GetColInt(dt, "washing_orders")
	ap.RechargeOrders = mysqlHelper.GetColInt(dt, "recharge_orders")
	ap.WashingPeer = mysqlHelper.GetColInt(dt, "washing_peer")
	ap.RechargePeer = mysqlHelper.GetColInt(dt, "recharge_peer")

	washingTotal := mysqlHelper.GetColInt(dt, "washing_total")
	ap.WashingTotal = fmt.Sprintf("%.2f", float32(washingTotal)/float32(100))
	rechargeTotal := mysqlHelper.GetColInt(dt, "recharge_total")
	ap.RechargeTotal = fmt.Sprintf("%.2f", float32(rechargeTotal)/float32(100))
	total := mysqlHelper.GetColInt(dt, "total")
	ap.Total = fmt.Sprintf("%.2f", float32(total)/float32(100))
	washingDiscount := mysqlHelper.GetColInt(dt, "washing_discount")
	ap.WashingDiscount = fmt.Sprintf("%.2f", float32(washingDiscount)/float32(100))
	rechargeDiscount := mysqlHelper.GetColInt(dt, "recharge_discount")
	ap.RechargeDiscount = fmt.Sprintf("%.2f", float32(rechargeDiscount)/float32(100))
	washingCashDiscount := mysqlHelper.GetColInt(dt, "washing_cash_discount")
	ap.WashingCashDiscount = fmt.Sprintf("%.2f", float32(washingCashDiscount)/float32(100))
	washingQBDiscount := mysqlHelper.GetColInt(dt, "washing_qb_discount")
	ap.WashingQBDiscount = fmt.Sprintf("%.2f", float32(washingQBDiscount)/float32(100))
	washingCashPart := mysqlHelper.GetColInt(dt, "washing_cash_part")
	ap.WashingCashPart = fmt.Sprintf("%.2f", float32(washingCashPart)/float32(100))
	washingQBPart := mysqlHelper.GetColInt(dt, "washing_qk_part")
	ap.WashingQBPart = fmt.Sprintf("%.2f", float32(washingQBPart)/float32(100))
	return ap
}
